package net.modul.district.dao.impl;

import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.modul.district.dao.DistrictDao;
import net.modul.district.vo.DistrictVo;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class DistrictDaoImpl extends GeneralDaoImpl implements DistrictDao {

	@Override
	public List<DistrictVo> search(DistrictVo bean) throws Exception {
		String sql = "select district_id,district_name,full_name,area_code,district_code,parent_code,flag from t_district where flag=0";
		if (bean.getParent_code() != null) {
			sql += " and parent_code=?";
		} else {
			sql += " and parent_code is null";
		}
		ResultSetHandler<List<DistrictVo>> rsh = new BeanListHandler<DistrictVo>(
				DistrictVo.class);
		return gqr.queryByBean(sql, rsh);
	}

	@Override
	public void insert(DistrictVo bean) throws Exception {
		String sql = "insert into t_district (district_name,full_name,area_code,district_code,parent_code,flag) values (?,?,?,?,?,?)";
		gqr.insert(sql, bean);
	}

	@Override
	public void update(DistrictVo bean) throws Exception {
		String sql = "update t_district set district_name=?,full_name=?,area_code=? where district_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public void updateFlag(DistrictVo bean) throws Exception {
		String sql = "update t_district set flag=? where district_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public DistrictVo find(Integer id) throws Exception {
		String sql = "select district_id,district_name,full_name,area_code,district_code,parent_code,flag from t_district where district_id=?";
		ResultSetHandler<DistrictVo> rsh = new BeanHandler<DistrictVo>(
				DistrictVo.class);
		return gqr.query(sql, rsh, new Object[] { id });
	}
}
